﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVCTutorial.Core.Model;

namespace MVCTutorial.Core.Services
{
	public class UserService
	{
		internal UserService() { }

		public IEnumerable<User> Search(string namelike)
		{
			return User.Search(namelike);
		}

		public IEnumerable<User> GetAll()
		{
			return User.GetAll();
		}

		public void Create(string name, int?[] skillIds)
		{
			User user = new User(name);

			if (skillIds != null)
			{
				int[] ids = (from i in skillIds select i.Value).ToArray();
				user.SetSkills(ids);
			}
		
			user.Save();
		}

		public void Update(int userId, string name, int?[] skillIds)
		{
			User user = User.GetById(userId);

			if (user == null)
				return;

			user.Name = name;

			if (skillIds != null)
			{
				int[] ids = (from i in skillIds select i.Value).ToArray();
				user.SetSkills(ids);
			}
			else
			{
				user.SetSkills(new int[]{});	
			}

			user.Save();
		}

		public void Delete(int userId)
		{
			User.Delete(userId);
		}

		public User GetById(int id)
		{
			return User.GetById(id);
		}
	}
}